package com.hzgj.soa.spring.schema;

import com.hzgj.bcl.soa.config.ReferenceDef;
import com.hzgj.bcl.soa.monitor.MonitorService;
import com.hzgj.bcl.soa.util.NetUtils;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
 * Created by Administrator on 2016/8/17.
 */
public class MonitorBeanDefinitionParser extends SoaNamespaceHandler.CommonBeanDefinitionParser {

    public static final String REFERENCE_DEF_MONITOR = "referenceDef#Monitor";
    public static final String BEAN_DEF_MONITOR = "MonitorBean#";

    public MonitorBeanDefinitionParser(Class<?> beanClass) {
        super(beanClass, false);
    }

    @Override
    void parse0(RootBeanDefinition beanDefinition, Element element, ParserContext parserContext, Class<?> beanClass) {
        super.parse0(beanDefinition, element, parserContext, beanClass);

        RootBeanDefinition referenceBeanDefinition = new RootBeanDefinition();
        referenceBeanDefinition.setBeanClass(ReferenceDef.class);
        referenceBeanDefinition.setLazyInit(false);

        String id = (String) beanDefinition.getPropertyValues().getPropertyValue("id").getValue();

        referenceBeanDefinition.getPropertyValues().add("id", id);
        referenceBeanDefinition.getPropertyValues().add("service", MonitorService.class.getName());
        referenceBeanDefinition.getPropertyValues().add("check", false);
        referenceBeanDefinition.getPropertyValues().add("host", NetUtils.getAndCheckHost(null));
        parserContext.getRegistry().registerBeanDefinition(REFERENCE_DEF_MONITOR, referenceBeanDefinition);
        beanDefinition.getPropertyValues().addPropertyValue("referenceDef", new RuntimeBeanReference(REFERENCE_DEF_MONITOR));
    }

    @Override
    protected String getId() {
        return BEAN_DEF_MONITOR;
    }
}
